package cn.lhl.web.cargo;

import cn.lhl.domain.cargo.*;
import cn.lhl.domain.vo.ExportProductVo;
import cn.lhl.domain.vo.ExportResult;
import cn.lhl.domain.vo.ExportVo;
import cn.lhl.service.cargo.ContractService;
import cn.lhl.service.cargo.ExportProductService;
import cn.lhl.service.cargo.ExportService;
import cn.lhl.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.apache.cxf.jaxrs.client.WebClient;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RequestMapping("/cargo/export")
@Controller
public class ExportController extends BaseController {

    @Reference
    private ContractService contractService;

    @Reference
    private ExportService exportService;

    @Reference
    private ExportProductService exportProductService;

    @RequestMapping(value = "/contractList", name = "合同管理列表界面")
    public String findAll(@RequestParam(defaultValue = "1") int page,
                          @RequestParam(defaultValue = "5") int size) {
        //分页查询所有合同
        ContractExample contractExample = new ContractExample();
        ContractExample.Criteria exampleCriteria = contractExample.createCriteria();
        exampleCriteria.andCompanyIdEqualTo(companyId);
        exampleCriteria.andStateEqualTo(1);
        PageInfo pageInfo = contractService.findAll(contractExample, page, size);
        model.addAttribute("page", pageInfo);
        return "/cargo/export/export-contractList";
    }

    @RequestMapping(value = "/toExport")
    public String toExport(String id) {
        request.setAttribute("id", id);
        return "cargo/export/export-toExport";
    }

    @RequestMapping(value = "/list", name = "已提交的报运单展示")
    public String list(@RequestParam(defaultValue = "1") int page,
                       @RequestParam(defaultValue = "5") int size) {
        //分页查询报运单
        ExportExample exportExample = new ExportExample();
        ExportExample.Criteria exportExampleCriteria = exportExample.createCriteria();
        exportExampleCriteria.andCompanyIdEqualTo(companyId);

        PageInfo pageInfo = exportService.findAll(exportExample, page, size);
        model.addAttribute("page", pageInfo);
        return "cargo/export/export-list";
    }

    @RequestMapping(value = "/edit", name = "新增报运单或更新报运单")
    public String edit(Export export) {
        //先判断是否有id(新增还是修改)
        if (StringUtil.isEmpty(export.getId())) {
            //新增
            //为该实体类赋id
            export.setId(UUID.randomUUID().toString());
            //赋值
            export.setCompanyId(companyId);
            export.setCompanyName(companyName);
            //保存
            exportService.save(export);
        } else {
            exportService.update(export);
        }
        //去已保存的的报运单列表
        return "redirect:/cargo/export/list.do";
    }

    @RequestMapping(value = "/toUpdate")
    public String toUpdate(String id) {

        //1.通过id查找报运主表实体类
        Export export = exportService.findById(id);

        //2.查找报运商品列表
        ExportProductExample example = new ExportProductExample();
        ExportProductExample.Criteria criteria = example.createCriteria();
        criteria.andExportIdEqualTo(id);
        List<ExportProduct> list = exportProductService.findAll(example);

        //3.将export,eps放入request域当中
        request.setAttribute("export", export);
        request.setAttribute("eps", list);

        //4.跳转页面
        return "cargo/export/export-update";
    }

    @RequestMapping(value = "/submit", name = "提报运单改变包运单状态")
    public String submit(String id) {
        Export export = exportService.findById(id);
        export.setState(1);
        exportService.update(export);
        return "redirect:/cargo/export/list.do";
    }

    @RequestMapping(value = "/exportE", name = "电子报运")
    public String exportE(String id) {
        //创建exportVo实体类
        ExportVo exportVo = new ExportVo();
        //通过id查询export实体类
        Export export = exportService.findById(id);
        //通过export实体类向exportVo中的部分字段赋值
        BeanUtils.copyProperties(export, exportVo);
        //需要向该实体类的exportid赋值做到关联关系
        exportVo.setExportId(id);
        //创建ExportProductVo列表
        List<ExportProductVo> exportProductVoList = new ArrayList<ExportProductVo>();
        //通过exportid字段查询得出ExportProduct列表
        ExportProductExample exportProductExample = new ExportProductExample();
        ExportProductExample.Criteria epExampleCriteria = exportProductExample.createCriteria();
        epExampleCriteria.andExportIdEqualTo(id);
        List<ExportProduct> productList = exportProductService.findAll(exportProductExample);
        //循环遍历列表得到ExportProduct向ExportProductVo列表赋值
        for (ExportProduct product : productList) {
            ExportProductVo exportProductVo = new ExportProductVo();
            BeanUtils.copyProperties(product,exportProductVo);
            //给各个id赋值
            exportProductVo.setExportId(id);
            exportProductVo.setExportProductId(product.getId());
            exportProductVoList.add(exportProductVo);
        }
        //通过得到ExportProductVo列表向exportVo实体类中的products字段赋值
        exportVo.setProducts(exportProductVoList);
        //调用海关接口
        //调用webservice接口
        //传送数据
        WebClient wb = WebClient.create("http://localhost:8088/ws/export/user");
        wb.post(exportVo);

        //接收数据
        wb = WebClient.create("http://localhost:8088/ws/export/user/"+id);
        ExportResult exportResult = wb.get(ExportResult.class);

        System.out.println(exportResult);
        //更新数据库中的数据
        exportService.updateE(exportResult);

        return "redirect:/cargo/export/list.do";
    }
}
